package com.jxpanda.spring.module.pay.core.client.wechat;

import com.jxpanda.spring.module.pay.config.properties.WechatPayProperties;
import com.jxpanda.spring.module.pay.core.client.ReactivePaymentClient;
import com.jxpanda.spring.module.pay.core.pojo.PaymentCommand;
import com.jxpanda.spring.module.pay.core.pojo.PaymentRequest;
import com.jxpanda.spring.module.pay.core.pojo.PaymentResponse;
import com.jxpanda.spring.module.pay.core.pojo.PaymentChannel;
import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.service.payments.jsapi.JsapiService;
import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
import com.wechat.pay.java.service.payments.jsapi.model.Amount;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayRequest;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayResponse;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayWithRequestPaymentResponse;
import lombok.Getter;
import reactor.core.publisher.Mono;

public class WechatReactivePaymentClient implements ReactivePaymentClient {

    @Getter
    private final String appName;

    private final WechatPayProperties.Applet applet;

    private final Config config;

    public WechatReactivePaymentClient(String appName, WechatPayProperties.Applet applet) {
        this.appName = appName;
        this.applet = applet;
        this.config = new RSAAutoCertificateConfig.Builder()
                .merchantId(applet.getMerchantId())
                .privateKeyFromPath(applet.getPrivateKeyPath())
                .merchantSerialNumber(applet.getMerchantSerialNumber())
                .apiV3Key(applet.getApiV3key())
                .build();
    }

    @Override
    public String getAppId() {
        return applet.getAppId();
    }

    @Override
    public PaymentChannel getPaymentType() {
        return PaymentChannel.WECHAT_PAY;
    }

    @Override
    public <R, T> Mono<PaymentResponse<R>> execute(PaymentRequest<T> paymentRequest) {
        JsapiService jsapiService = new JsapiService.Builder().config(config).build();
        PrepayRequest prepayRequest = new PrepayRequest();
        prepayRequest.setAppid(applet.getAppId());
        prepayRequest.setMchid(applet.getMerchantId());
        Amount amount = new Amount();
        amount.setTotal(1);
        amount.setCurrency("CNY");
        prepayRequest.setAmount(amount);
        PrepayResponse prepay = jsapiService.prepay(prepayRequest);
        PaymentRequest.of(PrepayRequest.class)
                .data(prepayRequest);
        JsapiServiceExtension service = new JsapiServiceExtension.Builder().config(config).build();
        PrepayWithRequestPaymentResponse prepayWithRequestPaymentResponse = service.prepayWithRequestPayment(prepayRequest);
        return null;
    }


    private <T> void test(PaymentRequest<T> paymentRequest) {
        T data = paymentRequest.getData();
        PaymentCommand command = paymentRequest.getCommand();


    }


}
